package com.tenement.service.impl;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.tenement.base.constants.SysmConstants.state;
import com.tenement.base.exception.ParameterException;
import com.tenement.mybatis.dao.UserMapper;
import com.tenement.mybatis.entity.User;
import com.tenement.service.interfaces.IUserService;

@Service("IUserService")
public class UserServiceImpl implements IUserService {

	@Autowired
	private UserMapper userMapper;

	private static final Log logger = LogFactory.getLog(UserServiceImpl.class);

	/**
	 * 判断邮箱是否存在，用户修改密码
	 */
	@Override
	public boolean verifyEmailUsed(String email) {
		User user = userMapper.verifyEmailUsed(email);
		logger.info("【-查询邮箱user==null-】 " + (user == null) + ";");
		if (user != null) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 判断手机号是否已经被注册
	 */
	@Override
	public boolean verifyPhoneUsed(String uTelephone) {
		logger.info("【校验手机号是否已被注册-业务层】");
		User user = userMapper.verifyPhoneUsed(uTelephone);
		if (user != null) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 用户注册
	 */
	@Override
	public boolean userReister(User user) {
		int b = userMapper.userReister(user);
		if (b > 0) {
			return true;
		} else {
			return false;
		}

	}

	/**
	 * 邮箱登录
	 */
	@Override
	public String userLoginByEmail(User user, HttpServletRequest request) {
		User u = userMapper.userLoginByEmail(user);
		if (u != null) {
			saveUseToSession(request, u);// 保存用户信息
			return state.Success;
		} else {
			return state.Failed;
		}
	}

	/**
	 * 手机号登录
	 */
	@Override
	public String userLoginByPhone(User user, HttpServletRequest request) {
		User u = userMapper.userLoginByPhone(user);
		if (u != null) {
			saveUseToSession(request, u);// 保存用户信息
			return state.Success;
		} else {
			return state.Failed;
		}
	}

	/**
	 * 修改用户密码
	 */
	@Override
	public boolean modifyPwd(String uId, String pwd) {
		User user = new User();
		user.setuId(Integer.parseInt(uId));
		user.setuPassword(pwd);
		int i = userMapper.modifyUserPassword(user);
		if(i>0){
			return true;
		}else{
			return false;	
		}
	}

	/**
	 * 修改用户手机号
	 */
	@Override
	public void modifyUserPhone(User user, HttpServletRequest request) {
		int i = userMapper.modifyUserPhone(user);
		if (i > 0) {
			saveUseToSession(request, userMapper.selectByPrimaryKey(user.getuId()));// 保存用户信息
			logger.info("【修改手机号成功】");
		} else {
			logger.error("【修改手机号失败】");
		}
	}

	/**
	 * 修改用户头像和用户名
	 */
	@Override
	public boolean modifyUserMsg(User user, HttpServletRequest request) {
		try {
			if (user == null) {
				throw new ParameterException("【修改用户信息参数为空】");
			}
			int i = userMapper.modifyUserMsg(user);
			if (i > 0) {
				saveUseToSession(request, userMapper.selectByPrimaryKey(user.getuId()));// 保存用户信息
				return true;
			} else {
				return false;
			}

		} catch (ParameterException e) {
			e.printStackTrace();
		}
		return false;
	}

	/**
	 * 修改和绑定用户邮箱
	 */
	@Override
	public void modifyUserEmail(User user,HttpServletRequest request) {
		int i = userMapper.modifyUserEmail(user);
		if (i > 0) {
			saveUseToSession(request, userMapper.selectByPrimaryKey(user.getuId()));// 保存用户信息
			logger.info("【修改用户邮箱成功】");
		} else {
			logger.error("【修改用户邮箱失败】");
		}
	}

	/**
	 * 获取用户信息
	 */
	@Override
	public List<User> getUserList() {
		return userMapper.getUserList();
	}

	@Override
	public boolean deleteUserByKey(int uId) {
		return false;
	}

	/**
	 * 登录时，保存修改时保存用户信息，
	 * 
	 * @param request
	 * @param user
	 */
	public static void saveUseToSession(HttpServletRequest request, User user) {
		HttpSession session = request.getSession();
		session.removeAttribute("USERMSGKEY");
		session.setAttribute("USERMSGKEY", user);
	}
}
